// Class for adding extra functionality to WordPress post administration

var postAdmin = (function(){
	var _current_category='thumbnail', // thumbnail is the default category
		_id=0, // _id of 0 represents no image
		_observer; // store process for setMediaListeners
	
	return{
		init: function(){
			postAdmin.setListeners();
			jQuery('#add_image').click(postAdmin.mediaListeners); // setup listeners for media center
			
			postAdmin.selectTypeKey(); // initialize page, highlighting selected image
		},
		
		setListeners: function(){
			jQuery('div.op_image_selection_img').click(postAdmin.onSelect);
			jQuery('select#op_img_type').keyup(postAdmin.selectTypeKey); // listen to key changing dropdown
			jQuery('select#op_img_type').change(postAdmin.selectType); // listen to dropdown changing
			jQuery('a#op_img_clear_trigger').click(postAdmin.clearType); // clear image
		},
		
		/**
		 * mediaListeners
		 * User has opened WP media upload center
		 * Setup listener to find out if the media center has closed
		 **/
		mediaListeners: function(){
			// Setup periodical listener for media
			_observer = OPW.periodicalExecuter.call();
			_observer.init(postAdmin.setMediaListeners,.5);
		},
		
		setMediaListeners: function(){
			if(jQuery('#TB_title').length != 0){ // detect that media library window is open
				_observer.stop();
				_observer = OPW.periodicalExecuter.call();
				_observer.init(postAdmin.setUploadMediaListeners,.5);
			}
		},
		
		setUploadMediaListeners: function(){
			if(jQuery('#TB_title').length == 0){ // detect that media library window is closed
				_observer.stop();
				postAdmin.newImages(); // refresh images
			}
		},

		/**
		 *  This is called when the user has closed the media window
		 *  Refreshes images displayed in OP Image Management box
		 **/
		newImages: function(){
			var post_id;
			
			post_id = jQuery(':input#post_ID').val(); // get the post's ID
			// using the post's ID, get all images, using AJAX, for the post
			jQuery.ajax({
				type: 'post',
				url: 'admin-ajax.php',
				data: 'action=op_img_get&post_id='+post_id,
				success: postAdmin.displayImages // upon completing, display images associated with this post
			});
		},
		
		/**
		 * displayImages
		 * Display images for post
		 * Triggered by postAdmin.newImages AJAX onSuccess
		 **/
		displayImages: function(e){
			var response = e.substring(0,e.length-1); // remove WP garbage 0 from string
			jQuery('#op_img_wrap').html(response); // update op_img_wrap html with new images
			postAdmin.setListeners(); // re-initialize listeners since there may be more/fewer images
		},
		
		/**
		 * selectTypeKey
		 * Listen to user changing dropdown value using keyboard instead of mouse
		 * This is required because the keyboard does not trigger onChange on all browsers
		 **/
		selectTypeKey: function(){
			// find out which option is currently selected
			jQuery('select#op_img_type option').each(function(){
				if(this.selected){
					_current_category = jQuery(this).val(); // set _current_category to selected type
				}
			})
			_id = jQuery(':input#op_img_'+_current_category).val(); // get the current image id for selected category
			postAdmin.selectImage(); // highlight image
		},
		
		/**
		 * selectType
		 * Triggered by dropdown onChange
		 **/
		selectType: function(e){
			var target = e.srcElement || e.currentTarget;
				
			_current_category = jQuery(target).val(); // assign category based on dropdown value
			_id = jQuery(':input#op_img_'+_current_category).val(); // get current image id for this category
			postAdmin.selectImage(); // highlight image
			
		},
		
		/**
		 * Triggered by clicking on an image
		 **/
		onSelect: function(e){
			var target = e.srcElement || e.currentTarget; // get click event
			
			_id = target.id.split('-')[1]; // image id's are structured as i.e. image-1
			_id = _id == 0 ? '' : _id; // if user has selected no image, leave field blank
			jQuery(':input#op_img_'+_current_category).eq(0).val(_id); // set hidden input value to image id
			postAdmin.selectImage(); // highlight image
		},
		
		selectImage: function(){
			_id = _id == '' ? 0 : _id; // if user has selected no image, leave field blank
		
			// adjust image classes based on newly selected image	
			jQuery('div.op_image_selection_img').each(function(){
				if(this.id.split('-')[1] == _id){
					jQuery(this).children().eq(0).addClass('selected');
				}
				else{
					jQuery(this).children().eq(0).removeClass('selected');
				}
			});
		}		
	}
})();

jQuery(document).ready(postAdmin.init);